package com.wwwarehouse.contract.release_supply_requirements;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.wwwarehouse.common.activity.BaseCardDeskActivity;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.eventbus_event.CardDeskEvent;

import contract.wwwarehouse.com.contract.R;
import de.greenrobot.event.EventBus;

/**
 * @创建者 chenchao.
 * @创建时间 2017/9/25 12:54
 * @描述 ${TODO}
 */
@Route(path = "/contract/PublishSuccessFragment")
public class PublishSuccessFragment extends BaseFragment implements View.OnClickListener {

    private View mView;
    private TextView mTvPublishedNum;
    private Button mBtSeePublished;
    private Button mBtFinish;
    private int mPublishNum;
    private long mBuId;
    private Bundle mSuccessToPublishBundle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_publish_success, container, false);
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        mSuccessToPublishBundle = new Bundle();//传到下一个界面的bundle
        Bundle publishNumBundle = getArguments();
        if (publishNumBundle != null) {
            mPublishNum = publishNumBundle.getInt("PublishNum");
            mBuId = publishNumBundle.getLong("buId");
        }
        if (mPublishNum != 0) {
            mTvPublishedNum.setText(mActivity.getString(R.string.total_publish) + mPublishNum + mActivity.getString(R.string.num_resourse));
        }

    }

    private void initView() {
        mTvPublishedNum = findView(mView, R.id.tv_published_num);
        mBtSeePublished = findView(mView, R.id.bt_see_published);
        mBtSeePublished.setOnClickListener(this);
        mBtFinish = findView(mView, R.id.bt_finish);
        mBtFinish.setOnClickListener(this);
        ((BaseCardDeskActivity) mActivity).hideBackBt();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ((BaseCardDeskActivity) mActivity).showBackBt();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(String string) {
        if ("SEELOCAL_PUBLISH_TO_SUCCESS".equals(string)) {
            ((BaseCardDeskActivity) mActivity).hideBackBt();
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof PublishSuccessFragment)
            mActivity.setTitle(mActivity.getResources().getString(R.string.publish_re));
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.bt_see_published) {
            mSuccessToPublishBundle.putLong("buId", mBuId);
//            popFragment();
            ((BaseCardDeskActivity) mActivity).showBackBt();
            pushFragment("/contract/SeeLocalPublishResourseFragment", mSuccessToPublishBundle);
        } else if (id == R.id.bt_finish) {
            EventBus.getDefault().post(new CardDeskEvent("clear_back_stack"));
        }
    }
}
